<?php
class comments{
    var $page;
    var $article;
    var $user;
    var $head;
    var $os = null;
    var $author = null;
    var $time = null;
    var $body = null;

    function set_page($new_page){
        $this->pageid = $new_page;
        return $this->pageid;
    }

    function set_id($new_id){
        $this->id = $new_id;
        return $this->id;
    }

    function set_user($new_user){
        $this->user = $new_user;
        return $this->user;
    }

    function get_head(){
        if($this->pageid != "14"){ //userposts
            $this->head = "Comments";
        }
        else{
            $this->head = "All comments by ".ucfirst($this->user);
        }
        return $this->head;
    }

    function get_comments(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($this->pageid != "14"){ //userposts
            $commentQuery = $connect->query("SELECT * FROM ".$prefix."comments WHERE page = '".$this->pageid."' AND postid = '".$this->id."' ORDER BY time DESC");
        }
        else{
            $commentQuery = $connect->query("SELECT * FROM ".$prefix."comments WHERE author = '".$this->user."' ORDER BY time DESC");
        }
        while($commentRow = $commentQuery->fetch_object()){

            if($commentRow->author == $_SESSION['username']){
                $author = "You";
            }
            else{
                $author = $commentRow->author;
            }

            $userQuery = $connect->query("SELECT os FROM ".$prefix."users WHERE username = '".$commentRow->author."'");
            $userRow = $userQuery->fetch_object();
            if($userRow->os != ""){
                $os = '| '.$userRow->os.' user';
            }
            else{
                $os = "";
            }

            $articleQuery = $connect->query("SELECT id FROM ".$prefix."users WHERE username = '".$commentRow->author."'");
            $articleRow = $articleQuery->fetch_object();

            $time = date('l F jS Y m:s',strtotime($commentRow->time));

            $this->comments[] = array(
                "author" => $author,
                "id" => $articleRow->id,
                "user" => $commentRow->author,
                "time" => $time,
                "os" => $os,
                "body" => $commentRow->body
            );
        }
        return $this->comments;
        $articleQuery->close();
        $userQuery->close();
        $commentQuery->close();
        $connect->close();
    }
}

?>
